package cn.iocoder.yudao.module.design.service.relativednc;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.design.controller.admin.relativednc.vo.*;
import cn.iocoder.yudao.module.design.convert.relativednc.RelativedNcConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.relatived.RelativedDO;
import cn.iocoder.yudao.module.design.dal.dataobject.relativednc.RelativedNcDO;
import cn.iocoder.yudao.module.design.dal.mysql.relatived.RelativedMapper;
import cn.iocoder.yudao.module.design.dal.mysql.relativednc.RelativedNcMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.*;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.design.enums.ErrorCodeConstants.RELATIVED_NC_NOT_EXISTS;

/**
 * 关联人表请求 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class RelativedNcServiceImpl implements RelativedNcService {

    @Resource
    private RelativedNcMapper relativedNcMapper;

    @Resource
    private RelativedMapper relativedMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createRelativedNc(RelativedNcCreateReqVO createReqVO) {
        // 插入
        RelativedNcDO relativedNc = RelativedNcConvert.INSTANCE.convert(createReqVO);
        relativedNcMapper.insert(relativedNc);
        // 返回
        return relativedNc.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRelativedNc(RelativedNcUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateRelativedNcExists(updateReqVO.getId());
        // 更新
        RelativedNcDO updateObj = RelativedNcConvert.INSTANCE.convert(updateReqVO);
        relativedNcMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRelativedNc(Long id) {
        // 校验存在
        this.validateRelativedNcExists(id);
        // 删除
        relativedNcMapper.deleteById(id);
    }

    private void validateRelativedNcExists(Long id) {
        if (relativedNcMapper.selectById(id) == null) {
            throw exception(RELATIVED_NC_NOT_EXISTS);
        }
    }

    @Override
    public RelativedNcDO getRelativedNc(Long id) {
        return relativedNcMapper.selectById(id);
    }

    @Override
    public List<RelativedNcDO> getRelativedNcList(Collection<Long> ids) {
        return relativedNcMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<RelativedNcDO> getRelativedNcPage(RelativedNcPageReqVO pageReqVO) {
        return relativedNcMapper.selectPage(pageReqVO);
    }

    @Override
    public List<RelativedNcDO> getRelativedNcList(RelativedNcExportReqVO exportReqVO) {
        return relativedNcMapper.selectList(exportReqVO);
    }
    @Override
    public List<Map> getRelativedNcListByUserId( ) {
        final String loginUserId = SecurityFrameworkUtils.getLoginUserId().toString();

        return   relativedNcMapper.queryRelativedListByUserId(loginUserId);
    }

    @Override
    public List<Map> getRelativedNcReqListByUserId() {
        final String loginUserId = SecurityFrameworkUtils.getLoginUserId().toString();

        return   relativedNcMapper.queryRelativedReqListByUserId(loginUserId);
    }


    /**
     * 校验关联人表同意 处理中 请求是否存在
     *
     * @return 校验关联人表请求是否存在
     */
    @Override
    public int checkUserRelNcExists(String relId,String roleId) {
        final String userId = SecurityFrameworkUtils.getLoginUserId().toString();
        return relativedNcMapper.checkUserRelNcExists(userId,relId,roleId);
    }

    /**
     * 查询状态集合
     *
     * @param relId
     * @param roleId
     * @return Set<Integer>  statusList
     * @throws
     * @author Annie
     * @time 2023/9/27 17:21
     * @param[1] relId  关联人id
     * @param[2] roleId  关联人角色
     */
    @Override
    public Set<Integer> getUserRelStatusList(String relId, String roleId) {
        final String userId = SecurityFrameworkUtils.getLoginUserId().toString();
        return relativedNcMapper.getUserRelStatusList(userId,relId,roleId);

    }

    /**
     * 处理添加好友请求
     *
     * @author Annie
     * @time 2023/7/27 20:13
     * @param[1] RelativedNcDealVO vo
     * @throws
     */
    @Override
    public void dealRelativedNc(RelativedNcDealVO vo) {
        // 查询 请求是否存在   当前人id    userId  待处理  角色id
        final String relId = SecurityFrameworkUtils.getLoginUserId().toString();
        QueryWrapper<RelativedNcDO> query = new QueryWrapper<>();
        query.eq("user_id",vo.getUserId());
        query.eq("rel_id",relId);
        query.eq("role_id",vo.getRoleId());
        query.eq("status_cd","0");

        RelativedNcDO relativedNcDO = relativedNcMapper.selectOne(query);
        // 不存在 则数据不完整报错
        if(relativedNcDO==null){
            throw  new NullPointerException("数据不正确，请检查");
        }
        // 存在则  relatived 中添加  信息
        if("1".equals(vo.getStatusCd())){
            QueryWrapper<RelativedDO> queryWrap = new QueryWrapper<>();
            queryWrap.eq("user_id",vo.getUserId());
            queryWrap.eq("rel_user_id",relId);
            queryWrap.eq("relation_status","1");
            queryWrap.eq("rel_user_cd",vo.getRoleId());
            RelativedDO relativedDO1 = relativedMapper.selectOne(queryWrap);
            //  发起人正向数据

            relativedDO1.setRelationStatus("0");
            relativedMapper.updateById(relativedDO1);
        }

        // 关联人
/*        relativedDO = new RelativedDO();
        relativedDO.setNickName(vo.getRemarkName());
        relativedDO.setUserId(vo.getUserId());
        relativedDO.setRelUserId(relId);
        relativedDO.setRelUserCd(vo.getRoleId());
        relativedMapper.insert(relativedDO);*/
        //  修改 relativedNc 中状态 处理完成时间
        QueryWrapper<RelativedNcDO> updateWrapper = new QueryWrapper<>();
        relativedNcDO.setStatusCd(vo.getStatusCd());
        relativedNcDO.setFinishTime(new Date());
        updateWrapper.eq("user_id",relativedNcDO.getUserId());
        updateWrapper.eq("rel_id",relId);
        updateWrapper.eq("status_cd","1");
        updateWrapper.eq("role_id",relativedNcDO.getRoleId());
        relativedNcMapper.update(relativedNcDO,updateWrapper);
        
    }
}
